import { Component, OnInit, OnDestroy } from '@angular/core';
import { HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { Subscription } from 'rxjs/Subscription';
import { JhiEventManager, JhiAlertService } from 'ng-jhipster';

import { ShoppingCart } from './shopping-cart.model';
import { ShoppingCartService } from './shopping-cart.service';
import { Principal } from '../../shared';
import {routerNgProbeToken} from "@angular/router/src/router_module";
import {Router} from "@angular/router";
import * as url from "url";

@Component({
    selector: 'jhi-shopping-cart',
    templateUrl: './shopping-cart.component.html'
})
export class ShoppingCartComponent implements OnInit, OnDestroy {
shoppingCarts: ShoppingCart[];
    currentAccount: any;
    eventSubscriber: Subscription;
    totalmoney=0;

    constructor(
        private shoppingCartService: ShoppingCartService,
        private jhiAlertService: JhiAlertService,
        private eventManager: JhiEventManager,
        private principal: Principal,
        private router:Router
    ) {
    }

    loadAll() {
        this.shoppingCartService.query().subscribe(
            (res: HttpResponse<ShoppingCart[]>) => {
                console.log('res.bodyjjjjjjjjjjjjjjjjjjjjjjjjjj', res.body);
                this.shoppingCarts = res.body;
                if (this.shoppingCarts.length > 0) {
                    for(let i = 0; i < this.shoppingCarts.length; i++){
                        this.totalmoney += this.shoppingCarts[i].bookPrice * this.shoppingCarts[i].jhiNumber;
                        console.log("价格ggggg",this.shoppingCarts[i].bookPrice);
                    }
                    console.log("价格",this.totalmoney);
                } else {
                    this.totalmoney = 0;

                }
            },
            (res: HttpErrorResponse) => this.onError(res.message)
        );
    }
    ngOnInit() {
        this.loadAll();
        this.principal.identity().then((account) => {
            this.currentAccount = account;
        });
        this.registerChangeInShoppingCarts();

    }

    ngOnDestroy() {
        this.eventManager.destroy(this.eventSubscriber);
    }

    trackId(index: number, item: ShoppingCart) {
        return item.id;
    }
    registerChangeInShoppingCarts() {
        this.eventSubscriber = this.eventManager.subscribe('shoppingCartListModification', (response) => this.loadAll());
    }

    finish(){
        this.shoppingCartService.deleteByuser().subscribe((e) => {
            console.log('删除1', e);
                this.shoppingCartService.query().subscribe(
                    (res: HttpResponse<ShoppingCart[]>) => {
                        console.log('res.bodyjjjjjjjjjjjjjjjjjjjjjjjjjj', res.body);
                        this.shoppingCarts = res.body;
                        if (this.shoppingCarts.length > 0) {
                            for(let i = 0; i < this.shoppingCarts.length; i++){
                                this.totalmoney += this.shoppingCarts[i].bookPrice * this.shoppingCarts[i].jhiNumber;
                                console.log("价格ggggg",this.shoppingCarts[i].bookPrice);
                            }
                            console.log("价格",this.totalmoney);
                        } else {
                            this.totalmoney = 0;
                        }
                        this.router.navigateByUrl('/shopping-cart(popup:shopping-cart-new)');
                    },
                    (res: HttpErrorResponse) => this.onError(res.message)
                );
            console.log('删除2', e);
          /* this.router.navigateByUrl('/shopping-cart(popup:shopping-cart-new)');*/
        },
        (error) => {
        console.log(error);
        });

    }

    private onError(error) {
        this.jhiAlertService.error(error.message, null, null);
    }




}
